import Show from 'common/show.js'
import {
	apiBaseURL
} from 'common/config.js';

function checkModbile(mobile) {
	var re = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
	var result = re.test(mobile);
	if (!result) {
		return false; //若手机号码格式不正确则返回false
	}
	return true;
}

function checkusername(username) {
	var re = /^(?!_)(?!.*?_$)[a-zA-Z0-9_]*$/;
	var result = re.test(username);
	if (!result) {
		return false; //若手机号码格式不正确则返回false
	}
	return true;
}
function arraysEqual(arr1, arr2) {
  // 如果长度不同，直接返回 false
  if (arr1.length !== arr2.length) return false;

  // 逐个比较元素
  for (let i = 0; i < arr1.length; i++) {
    // 如果元素是对象或数组，递归比较
    if (Array.isArray(arr1[i]) && Array.isArray(arr2[i])) {
      if (!arraysEqual(arr1[i], arr2[i])) return false;
    } else if (arr1[i] !== arr2[i]) {
      return false;
    }
  }

  return true;
}

// 上传图片
function uploadImage(apiurl, filepath, callback) {
	let header = {
		'access_token': uni.getStorageSync('access_token'),
		'content-type': 'text/plain'
	}
	uni.uploadFile({
		url: apiBaseURL + apiurl,
		filePath: filepath,
		fileType: 'image',
		header: header,
		name: 'file',
		success: (uploadFileRes) => {
			callback(JSON.parse(uploadFileRes.data));
		},
		fail: (error) => {
			Show.show('图片上传失败')
		},
		complete: () => {
			setTimeout(function() {
				uni.hideLoading();
			}, 250);
		},
	});
}

function throttle(fn, gapTime) {
	if (gapTime == null || gapTime == undefined) {
		gapTime = 1500
	}

	let _lastTime = null

	// 返回新的函数
	return function() {
		let _nowTime = +new Date()
		if (_nowTime - _lastTime > gapTime || !_lastTime) {
			fn.apply(this, arguments) //将this和参数传给原函数
			_lastTime = _nowTime
		}
	}
}

// 转成树行结果
function treeDataTranslate(data, id = 'id', pid = 'pid', index) {
	var res = []
	var temp = {}
	var arr = uni.getStorageSync('myFamilyIndexs')
	let myName = uni.getStorageSync('username');

	for (var i = 0; i < data.length; i++) {
		temp[data[i][id]] = data[i]
		if (data[i].username == myName) {
			arr.push(index)
			uni.setStorageSync('myFamilyData', data[i])
		}
	}
	uni.setStorageSync('myFamilyIndexs', arr)

	for (var k = 0; k < data.length; k++) {
		if (!temp[data[k][pid]]) {
			pid = 'motherMemberId'
		}
		console.log(pid)
		if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
			if (!temp[data[k][pid]]['children']) {
				temp[data[k][pid]]['children'] = []
			}
			if (!temp[data[k][pid]]['_level']) {
				temp[data[k][pid]]['_level'] = 1
			}
			data[k]['_level'] = temp[data[k][pid]]._level + 1
			temp[data[k][pid]]['children'].push(data[k])
		} else {
			res.push(data[k])
		}
	}
	return res
}


// 转成树行结果
function treeDataTranslate2(data, id = 'id', pid = 'pid', index) {
	var res = []
	var temp = {}
	var arr = uni.getStorageSync('myFamilyIndexs')
	let myName = uni.getStorageSync('nickname');

	for (var i = 0; i < data.length; i++) {
		temp[data[i][id]] = data[i]
		if (data[i].nickname == myName) {
			arr.push(index)
			uni.setStorageSync('myFamilyData', data[i])
		}
	}
	uni.setStorageSync('myFamilyIndexs', arr)

	for (var k = 0; k < data.length; k++) {
		// if(!temp[data[k][pid]]){
		// 	pid = 'motherMemberId'
		// }
		console.log(pid)
		if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
			if (!temp[data[k][pid]]['children']) {
				temp[data[k][pid]]['children'] = []
			}
			if (!temp[data[k][pid]]['_level']) {
				temp[data[k][pid]]['_level'] = 1
			}
			data[k]['_level'] = temp[data[k][pid]]._level + 1
			temp[data[k][pid]]['children'].push(data[k])
		} else {
			res.push(data[k])
		}
	}
	return res
}
// 防止处理多次点击
function noMultipleClicks(methods, info) {
	// methods是点击后需要执行的函数， info是函数需要传的参数
	let that = this;
	if (that.noClick) {
		// 第一次点击
		that.noClick = false;
		if ((info && info !== '') || info == 0) {
			// info是执行函数需要传的参数
			methods(info);
		} else {
			methods();
		}
		setTimeout(() => {
			that.noClick = true;
		}, 2000)
	} else {
		//  这里是重复点击的判断
	}
}

function appendList(list, maxCount, newItems) {
  // 如果最多保留的条数是 -1，则直接追加所有新数据
  if (maxCount === -1) {
    list.push(...newItems);
    return list;
  }

  // 遍历新数据，逐条添加到列表
  for (const item of newItems) {
    // 添加新数据到列表尾部
    list.push(item);

    // 检查是否超过最多保留的条数
    if (list.length > maxCount) {
      // 如果超过，则删除列表开头的旧数据
      list.shift();
    }
  }

  return list;
}


export default {
	appendList: appendList,
	checkModbile,
	uploadImage,
	throttle: throttle,
	treeDataTranslate: treeDataTranslate,
	noMultipleClicks,
	treeDataTranslate2: treeDataTranslate2,
	checkusername:checkusername,
	arraysEqual:arraysEqual,
	vuemixin: {
		created: function() {
			console.log(1)
		}
	}
}